// src/models/User.ts
import { DataTypes, Model, Optional } from 'sequelize';
import sequelize from '../config/db';

// 定义用户模型接口
interface UserAttributes {
  user_id: number;
  username: string;
  password: string;
  name: string;
  avatar?: string;
  role_id: number;
  status: number;
}

interface UserCreationAttributes extends Optional<UserAttributes, 'user_id'> {}

class User extends Model<UserAttributes, UserCreationAttributes> implements UserAttributes {
  public user_id!: number;
  public username!: string;
  public password!: string;
  public name!: string;
  public avatar?: string;
  public role_id!: number;
  public status!: number;
}

User.init({
  user_id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  username: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true
  },
  password: {
    type: DataTypes.STRING(255),
    allowNull: false
  },
  name: {
    type: DataTypes.STRING(50),
    allowNull: false
  },
  avatar: {
    type: DataTypes.STRING(255),
    allowNull: true
  },
  role_id: {
    type: DataTypes.INTEGER,
    allowNull: false
  },
  status: {
    type: DataTypes.TINYINT(),
    defaultValue: 1  // 默认启用
  }
}, {
  tableName: 'users',
  sequelize,  // sequelize 实例
  timestamps: true
});

export default User;
